/*
 * @lc app=leetcode.cn id=78 lang=javascript
 *
 * [78] 子集
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function (nums) {
  let res = [[]];
  if (nums.length == 0) return res;
  let dfs = function (lastNums, index) {
      if(index==nums.length)return
    for (let i = index; i < nums.length; i++) {
      let temp = [...lastNums, nums[i]];
      res.push(temp);
      dfs(temp,i+1)
    }
  };
  dfs([], 0);
  return res;
};
// @lc code=end
var subsets = function(nums) {
    const ans = [];
    const n = nums.length;
    for (let mask = 0; mask < (1 << n); ++mask) {
        const t = [];
        for (let i = 0; i < n; ++i) {
            if (mask & (1 << i)) {//判断哪一位有
                t.push(nums[i]);
            }
        }
        ans.push(t);
    }
    return ans;
}//位运算，mask代表0-7的数，转为二进制再去其他即可